//
//  ViewController.m
//  FriendCircleDemo
//
//  Created by mac on 2018/9/25.
//  Copyright © 2018年 WY. All rights reserved.
//

#import "ViewController.h"
#import "WYMomentHeaderView.h"
#import "WYMoments.h"
#import "WYMomentFooterView.h"
#import "WYTableView.h"
#import "WYMomentContentCell.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)WYTableView *tableView;

@property (nonatomic, strong)NSMutableArray<WYMomentItemViewModel *> *dataSource;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.tableView];
    
//    NSInteger page = 1;
    for (NSInteger page = 1; page < 5; page ++) {
        
    NSData *data = [NSData dataNamed:[NSString stringWithFormat:@"WeChat_Moments_%zd.json",page]];
    WYMoments *momentsData = [WYMoments modelWithJSON:data];
    for (NSInteger i = 0; i < momentsData.moments.count; i++) {
        WYMoment *moment = momentsData.moments[i];
        WYMomentItemViewModel *itemViewModel = [[WYMomentItemViewModel alloc] initWithMoment:moment];
        [self.dataSource addObject:itemViewModel];
    }
    }
    NSLog(@"%@",self.dataSource);
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataSource.count;
}

/// 设置高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    WYMomentItemViewModel *itemViewModel = _dataSource[section];
    DDLog(@"%zd---%.0f",section,itemViewModel.height);
    return itemViewModel.height;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    WYMomentItemViewModel *itemViewModel = _dataSource[section];
    WYMomentHeaderView *headerView = [WYMomentHeaderView headerViewWithTableView:tableView];
    headerView.section = section;
    headerView.itemViewModel = itemViewModel;
    @weakify(self);
    headerView.reloadSection = ^(NSInteger section) {
        @strongify(self);
        
        [UIView performWithoutAnimation:^{
            [self.tableView reloadSection:section withRowAnimation:UITableViewRowAnimationAutomatic];
        }];
    };
    return headerView;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    WYMomentItemViewModel *itemModel = _dataSource[section];
    return itemModel.dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WYMomentItemViewModel *itemModel = _dataSource[indexPath.section];
    WYMomentContentItemViewModel *contentItemModel = itemModel.dataSource[indexPath.row];
    return contentItemModel.cellHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
//    if (!cell) {
//        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
//
//    }
//    cell.textLabel.text = [NSString stringWithFormat:@"%zd",indexPath.section];
    WYMomentItemViewModel *itemModel = _dataSource[indexPath.section];
    WYMomentContentItemViewModel *contentItemModel = itemModel.dataSource[indexPath.row];
    WYMomentContentCell *cell = [WYMomentContentCell cellWithTableView:tableView indexPath:indexPath];
    cell.contentItemModel = contentItemModel;
    return cell;
}
/// 点击Cell的事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

//-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
//{
//    return 0.5;
//}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [WYMomentFooterView footerViewWithTableView:tableView];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView bringSubviewToFront:cell];
    
}

-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[WYTableView alloc] initWithFrame:CGRectMake(0, statusBar_Height, Screen_Width, Screen_Height - statusBar_Height) style:UITableViewStyleGrouped];//[[UITableView alloc] initWithFrame:CGRectMake(0, statusBar_Height, Screen_Width, Screen_Height - statusBar_Height)];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.sectionFooterHeight = 0.5;
        if(@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }else{
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
    }
    return _tableView;
}

-(NSMutableArray *)dataSource
{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
@end
